<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理
use app\admin\model\File;

class Probrand extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) && $this->data['lang'] ? $this->data['lang'] : is_lang();
    }


    public function list(Request $request){
        if($request->isAjax()){
            $brandlist = getDatas('pro_brand',['*'],'1=1');
            $returnData['size'] = count( $brandlist);
            if(isset($this->data['page']) && isset($this->data['limit'])){
                if($this->data['limit']*($this->data['page']-1) < $returnData['size']){
                    $brandlist = array_slice($brandlist,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                    $returnData['page'] = $this->data['page'];
                }
            }
            $returnData['brandlist'] =$brandlist;
            return json_encode($returnData);
        }else{
            return view('brand');
        }
    }


    public function brandopen(){
        if($this->data['request'] == 'edit'){
            if(isset($this->data['id'])){
                $brandlist = getDatas('pro_brand',['*'],['id'=>$this->data['id']])[0];
                // dump($brandList);die;
                return view('brandopen',['title'=>lang('edit_product_brand'),'brandList'=>$brandList]);
            }
            
        }else if($this->data['request'] == 'add'){

            return view('brandopen',['title'=>lang('add')]);
        }else{
            return view('brandopen',['title'=>lang('product_brand').' '.lang('add')]);
        }
        
    }

    public function brandAdd(Request $request){
        // return $this->data;
        if($this->data['request'] == 'insert'){
            if(isset($this->data['img_path'])){
                $where['name'] = $this->data['name'];
                $is_has = hasData('pro_brand',$where);
                if($is_has){
                    return json_encode(array('status'=>3,'msg'=>lang('add_failed,_product_name_exists')));
                }else{
                    $res = insertData('pro_brand',['img_path'=>'/'.$this->data['img_path'],'name'=>$this->data['name']]);
                    if($res){ 
                        return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$this->data['name'].' '.lang('added_successfully')));
                    }else{
                        return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$this->data['name'].' '.lang('add_failed')));
                    }
                }
            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
        }else if($this->data['request'] == 'add'){
            return view('brandopen',['title'=>lang('add'),'type'=>'add']);

        }else if($this->data['request'] == 'uploads'){  // 上传图片
            if ($request->isPost()) {
                $fileUpdate = new File();
                $res = $fileUpdate->uploadImgs($request->file('file'),'uploads/static/Admin/img/probrand/');
                return json_encode($res);
            }
        } else{
            return view('brandopen',['title'=>lang('add'),'type'=>'add']);
        }
    }

    public function brandEdit(){
        if($this->data['request'] == 'update'){
        // return $this->data;
            if(!empty($this->data)){
                if(isset($this->data['img_path']) && $this->data['img_path'] != ''){
                    $img_path = '/'.$this->data['img_path'];
                    if(isset($this->data['old_img_path']) && $this->data['old_img_path'] != ''){
                        if(file_exists(ROOT_PATH . 'public' . DS . $this->data['old_img_path'])){
                            unset($image); 
                            $r = unlink(ROOT_PATH . 'public' . DS . $this->data['old_img_path']); 
                        }
                    }
                    $updateData['img_path'] = $img_path;
                }
                $updateData['name'] = $this->data['name'];
                $res = updateData('pro_brand',['id'=>$this->data['id']],$updateData);
                if($res >= 0){
                    return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$this->data['name'].' '.lang('edited_successfully')));
                }else{
                    return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$this->data['name'].' '.lang('edit_failed')));
                }

            }else{
                return json_encode(array('status'=>2,'msg'=>lang('improve_submission_data')));
            }
        }else if($this->data['request'] == 'edit'){
            if(isset($this->data['id'])){
                $brandList = getDatas('pro_brand',['*'],['id'=>$this->data['id']])[0];
                // dump($brandList);die;
                return view('brandopen',['title'=>lang('edit'),'type'=>'edit','brandList'=>$brandList]);
            }
        }
    }

            // 批量/单个删除
    public function batchDel()
    {
        return $this->changeStatus($this->data,$this->lang);
    }

    // 批量/单个恢复
    public function batchRestore()
    {
        return $this->changeStatus($this->data,$this->lang);
    }

    // 批量/单个清除
    public function batchClear()
    {
        return $this->changeStatus($this->data,$this->lang);
    }


    // 软删除/批量软删除，恢复/批量恢复，清除/批量清除
    public function changeStatus($data,$lang){
        if(!empty($data['id'])){  // 判断条件是否达成
            if($data['fun'] == 'update'){
                $updateData = ['status'=>$data['value']];
                Db::startTrans();
                try{
                    $content = '';
                    if(is_array($data['id'])){    // 是否批量删除
                        foreach ($data['id'] as $k => $v) {   
                            $content .= ','.getValue('pro_brand',['id'=>$v],'name');
                            $res = updateData($data['table'],['id'=>$v],$updateData); 
                        }
                        ltrim($content,',');
                        if($res){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$content.lang(getViewMultiLanguage($data['value'])).' '.lang('successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$content.lang(getViewMultiLanguage($data['value'])).' '.lang('failed')));
                        }
                    }else{
                        $content .= ','.getValue('pro_brand',['id'=>$data['id']],'name');
                        $res = updateData($data['table'],['id'=>$data['id']],$updateData); 
                        if($res){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$content.lang(getViewMultiLanguage($data['value'])).' '.lang('successfully')));
                        }else{
                            Db::rollback();
                            return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$content.lang(getViewMultiLanguage($data['value'])).' '.lang('failed')));
                        }
                    }
                }catch(\Exception $e){
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$e));
                }
            }else if($data['fun'] == 'deletes'){     // 清除

                Db::startTrans();
                try{
                    $content = '';
                    if(is_array($data['id'])){    // 是否批量删除
                        
                        foreach ($data['id'] as $k => $v) {  
                            // 判断是否绑定有产品
                            $is_has = hasData('pro',['brand_id'=>$v]);
                            // return $is_has;
                            if( empty($is_has) ){  
                                $content .= ','.getValue('pro_brand',['id'=>$v],'name');
                                $res1 = deleteData('pro_brand',['id'=>$v]);
                            }else{
                                return json_encode(array('status'=>2,'msg'=>lang("contains_product_and_can't_be_deleted")));
                            }
                        }

                        if($res1 >= 0){
                            Db::commit();
                            return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$content.' '.lang('cleared_successfully')));
                        }else{
                            return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$content.' '.lang('clear_failed')));
                        }
                        
                    }else{
                        // 判断是否绑定有产品
                        $is_has = hasData('pro',['brand_id'=>$data['id']]);
                        // return $is_has;
                        if( empty($is_has) ){  
                            $content .= ','.getValue('pro_brand',['id'=>$data['id']],'name');
                            $res1 = deleteData('pro_brand',['id'=>$data['id']]);
                            if($res1 >= 0){
                                Db::commit();
                                return json_encode(array('status'=>1,'msg'=>lang('product_brand').'->'.$content.' '.lang('cleared_successfully')));
                            }else{
                                return json_encode(array('status'=>0,'msg'=>lang('product_brand').'->'.$content.' '.lang('clear_failed')));
                            }
                        }else{
                            return json_encode(array('status'=>2,'msg'=>lang("contains_sublevel_doesn't_allow_deletion")));
                        }
                    }
                }catch(\Exception $e){
                    Db::rollback();
                    return json_encode(array('status'=>0,'msg'=>$e));
                }
            }
        }else{
            return json_encode(array('status' => 2,'msg'=>'请勾选需要操作的产品品牌'));
        }

    }
}
